package cn.stylefeng.guns.modular.school.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.school.entity.ExamInfo;
import cn.stylefeng.guns.modular.school.model.params.ExamInfoParam;
import cn.stylefeng.guns.modular.school.model.result.ClassTeacherLessionResult;
import cn.stylefeng.guns.modular.school.service.ClassTeacherLessionService;
import cn.stylefeng.guns.modular.school.service.ExamInfoService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * 考试信息控制器
 *
 * @author
 * @Date 2019-11-29 18:12:32
 */
@Controller
@RequestMapping("/examInfo")
public class ExamInfoController extends BaseController {

    private String PREFIX = "/school/examInfo";

    @Autowired
    private ExamInfoService examInfoService;
    @Autowired
    private ClassTeacherLessionService classTeacherLessionService;

    /**
     * 跳转到主页面
     *
     * @author
     * @Date 2019-11-29
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/examInfo.html";
    }

    /**
     * 新增页面
     *
     * @author
     * @Date 2019-11-29
     */
    @RequestMapping("/add")
    public String add() {
        // 取得当前登录账户担任教师的所有班级信息
        List<ClassTeacherLessionResult> currentUserClass = classTeacherLessionService.findCurrentUserClass();
        setAttr("classTeacherLessionResults", currentUserClass);
        return PREFIX + "/examInfo_add.html";
    }

    /**
     * 编辑页面
     *
     * @author
     * @Date 2019-11-29
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/examInfo_edit.html";
    }

    /**
     * 新增接口
     *
     * @author
     * @Date 2019-11-29
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ExamInfoParam examInfoParam) {
        this.examInfoService.add(examInfoParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author
     * @Date 2019-11-29
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ExamInfoParam examInfoParam) {
        this.examInfoService.update(examInfoParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author
     * @Date 2019-11-29
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ExamInfoParam examInfoParam) {
        this.examInfoService.delete(examInfoParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author
     * @Date 2019-11-29
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ExamInfoParam examInfoParam) {
        ExamInfo detail = this.examInfoService.getById(examInfoParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author
     * @Date 2019-11-29
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ExamInfoParam examInfoParam) {
        // 取得当前登录账户担任教师的所有班级信息
        List<ClassTeacherLessionResult> currentUserClass = classTeacherLessionService.findCurrentUserClass();
        // 没有班级的教师
        if (currentUserClass == null || currentUserClass.size() == 0) {
            LayuiPageInfo result = new LayuiPageInfo();
            result.setCount(0);
            result.setData(new ArrayList());
            return result;
        }
        List<Integer> classIdList = new ArrayList<>();
        Integer[] classIds = new Integer[currentUserClass.size()];
        currentUserClass.forEach(classTeacherLessionResult -> {
            classIdList.add(classTeacherLessionResult.getClassId());
        });
        classIdList.toArray(classIds);
        examInfoParam.setClassIds(classIds);
        return this.examInfoService.findPageBySpec(examInfoParam);
    }

}


